﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.System.Wf;
using System.Collections;
using Cn.Cellula.Client.ControlHelper;
using Cn.Cellula.Client;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Wf.Config {
    public partial class List : PageSupport {
        IWfBiz biz = BizFactory.Get<IWfBiz>();
        String WfKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            WfKey = Request.QueryString["wid"];
            if (!Page.IsPostBack) {
                InitForm();
                LoadWfList();
            }
        }

        private void LoadWfList() {
            String hql = "from TSysWfEO eo order by eo.WfName";
            this.gvSubList.DataSource = new ArrayList();
            this.gvSubList.DataBind();
            IList<TSysWfEO> list = biz.Find<TSysWfEO>(hql);
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();

            if (String.IsNullOrEmpty(WfKey)) {
                if (list != null && list.Count > 0) {
                    GvHelper.GvSelected(ref this.gvlist, list[0].WfKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                    LoadNodeList(list[0].WfKey);
                    this.hid_SelectedKey.Value = list[0].WfKey;
                } else {
                    this.btn_node_add.Enabled = false;
                    this.btn_node_delete.Enabled = false;
                    this.btn_node_update.Enabled = false;
                }
            } else {
                this.hid_SelectedKey.Value = WfKey;
                LoadNodeList(WfKey);
                GvHelper.GvSelected(ref this.gvlist, WfKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }

        }

        private void InitForm() {

        }


        private void LoadNodeList(String rWfKey) {
            this.hid_SelectedKey.Value = rWfKey;
            this.gvSubList.DataSource = biz.LoadSequenceNodeList(rWfKey);
            this.gvSubList.DataBind();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvlist.PageIndex = e.NewPageIndex;
            LoadWfList();
        }

        protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                //String key = gvlist.DataKeys[e.Row.RowIndex].Values[0].ToString();
                //string url = "Admin/Content/Code/_CodeList.aspx";
                //e.Row.Attributes.Add("url", url + "?ID=" + key);
                //e.Row.Attributes.Add("onclick", "return myUtil.onGridViewRowSelected(this,event,'cntrlist_container');");
            }
        }

        protected void btn_wf_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditWf.aspx");
        }

        protected void btn_wf_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditWf.aspx?id=" + keys[0] + "");
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_wf_delete_Click(object sender, EventArgs e) {
        }

        protected void btn_node_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditNode.aspx?wid=" + this.hid_SelectedKey.Value);
        }

        protected void btn_node_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvSubList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditNode.aspx?id=" + keys[0] + "");
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_node_delete_Click(object sender, EventArgs e) {
        }

        protected void gvlist_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckSubList") {
                String wfKey = e.CommandArgument.ToString();
                this.hid_SelectedKey.Value = wfKey;
                this.LoadNodeList(wfKey);
                GvHelper.GvSelected(ref this.gvlist, wfKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

    }
}